/**   
* @Title: ProductMonitorController.java 
* @Package com.csis.system.controller 
* @Description: TODO
* @author dailey  
* @date 2012-10-8
* @version V1.0   
*/
package com.csis.system.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.csis.framework.util.jqgrid.JqGridModel;
import com.csis.framework.util.jqgrid.JqGridUtils;
import com.csis.system.common.WebUtil;
import com.csis.system.domain.WareFormBean;
import com.csis.system.domain.bean.WarehouseBeanFormBean;
import com.csis.system.service.WarehouseService;

/**
 * @author dailey
 *
 */
@Controller
@RequestMapping("/system/monitor")
public class ProductMonitorController {
	protected Log log = LogFactory.getLog(getClass());
	private static final String BASE_URL = "/system/manager/sale/monitor/";
	@Resource
	private WarehouseService warehouseService;

	@RequestMapping("/toIndex")
	public String index() {
		return BASE_URL + "index";
	}

	@RequestMapping("/monitor")
	public String monitor() {
		return BASE_URL + "monitorware";
	}

	@RequestMapping("/monitorBy")
	public void monitorBy(JqGridModel model, HttpServletResponse response) {
		WareFormBean formBean = WebUtil.buildConvert(model).convertToSingle(WareFormBean.class);
		warehouseService.list(formBean);
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter os = null;
		try {
			os = response.getWriter();
			os.write(JqGridUtils.getJSONParser(WareFormBean.class).parse(formBean).getContent());
			os.flush();
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} finally {
			if (os != null)
				os.close();
		}
	}
	
	@RequestMapping("/monitorCountBy")
	public void monitorProductCount(JqGridModel model, HttpServletResponse response) {
		WarehouseBeanFormBean formBean = WebUtil.buildConvert(model).convertToSingle(WarehouseBeanFormBean.class);
		formBean.setWareId(model.getId());
		formBean.setMonitorCount(model.getMonitorCount());
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter os = null;
		try {
			warehouseService.listWareLeft(formBean);
			os = response.getWriter();
			os.write(JqGridUtils.getJSONParser(WarehouseBeanFormBean.class).parse(formBean).getContent());
			os.flush();
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} finally {
			if (os != null)
				os.close();
		}
	}

}
